package com.rmb.userinfo.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.rmb.userinfo.dao.LogRegisterDao;
import com.rmb.mart.entity.log.LogRegister;
import com.rmb.userinfo.excal.LogAddressExcal;
import com.rmb.userinfo.service.LogRegisterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (LogRegisterExcal)表服务实现类
 *
 * @author makejava
 * @since 2021-01-20 11:04:31
 */
@Service("logRegisterService")
public class LogRegisterServiceImpl implements LogRegisterService {
    @Resource
    private LogRegisterDao logRegisterDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LogRegister queryById(Long id) {
        return this.logRegisterDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LogRegister> queryAllByLimit(int offset, int limit) {
        return this.logRegisterDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param logRegister 实例对象
     * @return 实例对象
     */
    @Override
    public LogRegister insert(LogRegister logRegister) {
        this.logRegisterDao.insert(logRegister);
        return logRegister;
    }

    /**
     * 修改数据
     *
     * @param logRegister 实例对象
     * @return 实例对象
     */
    @Override
    public LogRegister update(LogRegister logRegister) {
        this.logRegisterDao.update(logRegister);
        return this.queryById(logRegister.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.logRegisterDao.deleteById(id) > 0;
    }

    /**
     * 查询所有日志记录
     * @return
     */
    @Override
    public List selectAll() {
        return logRegisterDao.selectAll();
    }

    /**
     * 导出Excal表
     */
    @Override
    public boolean exportExcal() {
        //相对路径
        String path = Thread.currentThread().getContextClassLoader()
                .getResource("com/rmb/entity/files").toString();
        List list = logRegisterDao.selectAll();
        ExcelWriterBuilder workBook = EasyExcel.write(path, LogAddressExcal.class);
        workBook.sheet().doWrite(list);
        return true;
    }
}